//
//  iMoneyAppDelegate.m
//  iMoney
//
//  Created by Sean McGrail on 1/29/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "iMoneyAppDelegate.h"
#import "OverviewViewController.h"
#import "NetSyncViewController.h"
#import "QuickViewController.h"
#import "AccountsViewController.h"
#import "OverlayView.h"


@implementation iMoneyAppDelegate

@synthesize window;
@synthesize tabBarController;

//main shared money state
static MoneyState* sharedMoneyState= nil;
static NSString* fileToSave = nil;


+(void) loadMoneyState
{
	//create money state
	sharedMoneyState = [[MoneyState alloc] init];
	

	NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString* docs = [paths objectAtIndex:0];
	fileToSave = [[docs stringByAppendingPathComponent:@"data.xml"] retain];
	
	if(fileToSave != NULL){
	//load data
	[sharedMoneyState parseXmlFromFile:fileToSave];
	}
	

}

+(NSString*)getFilePath
{
	return fileToSave;
}



+(MoneyState*)getMoneyState
{
	if(sharedMoneyState== nil)
	{
		[iMoneyAppDelegate loadMoneyState];
	}

	
	return sharedMoneyState;
}


- (void) toggleTabBar {
	
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
	
	
    for(UIView *view in tabBarController.view.subviews)
    {
		
        if([view isKindOfClass:[UITabBar class]])
        {
			
            if (hiddenTabBar) {
                [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
            }
        } else {
            if (hiddenTabBar) {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
            }
			
        }
    }
	
    [UIView commitAnimations];
	
    hiddenTabBar = !hiddenTabBar;
}

- (void) toggleNavBar {
	
    [accountNav setNavigationBarHidden:!hiddenNavBar animated:YES];
	
    hiddenNavBar = !hiddenNavBar;
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    
	
	[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
	
	
	//[[UIApplication sharedApplication] setApplicationIconBadgeNumber:7];
	
	tabBarController = [[UITabBarController alloc] init];  
	
	
	OverviewViewController* overview = [[OverviewViewController alloc] init];
	NetSyncViewController* netSync = [[NetSyncViewController alloc] init];
	QuickViewController* quick = [[QuickViewController alloc] initWithNibName:@"QuickViewController" bundle:[NSBundle mainBundle]];
	AccountsViewController* acc = [[AccountsViewController alloc] init];
	
	
	accountNav = [[UINavigationController alloc] initWithRootViewController:acc];
	accountNav.navigationBar.barStyle =
	UIBarStyleBlackOpaque; 	
	
	tabBarController.viewControllers = [NSArray arrayWithObjects:accountNav, quick, overview, netSync, nil]; 
	
	//release controllers
	[quick release];
	[overview release];
	[netSync release];
	[acc release];
	
	[accountNav release];
	
	//see if the user wants a lock screen
	NSString *lockValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"lock_screen"];
	if(lockValue != NULL)
	{
		if([lockValue boolValue])
		{
			lockScreen = [[LockScreenView alloc] initLockScreen:self:NULL];
			[window addSubview:lockScreen];
			[lockScreen release];
			return;
		}
		
	}
		
	
	//load state
	[iMoneyAppDelegate loadMoneyState];
	[window addSubview:tabBarController.view];

}


-(void)inputSucceded
{

	// Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];
	
	//show the loading on the data
	//load state
	[iMoneyAppDelegate loadMoneyState];
	
	[lockScreen removeFromSuperview];
}




- (void)applicationWillTerminate:(UIApplication *)application {
	
	
	//if(fileToSave != nil){
	// Save data
	//	[sharedMoneyState saveDataFile:fileToSave];
	//}
}

/*
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
}
*/


- (void)dealloc {
	[fileToSave release];
	[sharedMoneyState release];
    [tabBarController release];
    [window release];
    [super dealloc];
}

@end

